package com.example.forum.controller;

import com.example.forum.common.AppResult;
import com.example.forum.common.ResultCode;
import com.example.forum.config.AppConfig;
import com.example.forum.model.Article;
import com.example.forum.model.ArticleReply;
import com.example.forum.model.User;
import com.example.forum.service.ArticleReplyService;
import com.example.forum.service.ArticleService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Update;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/reply")
public class ArticleReplyController {
    @Resource
    private ArticleService articleService;
    @Resource
    private ArticleReplyService articleReplyService;

    @RequestMapping("/create")
    public AppResult create(HttpServletRequest request,
                            @RequestParam("articleId") @NonNull long articleId,
                            @RequestParam("content") @NonNull  String content){

        // 获取用户
        HttpSession session=request.getSession(false);
        User user=(User) session.getAttribute(AppConfig.USER_SESSION);
        // 判断用户是否已禁言
        if (user.getState()==1){
            // 表示已禁言
            return AppResult.failed(ResultCode.FAILED_USER_BANNED);
        }
        // 获取要回复的帖子对象
        Article article=articleService.selectById(articleId);
        // 是否删除，或已删除
        if (article==null || article.getDeleteState()==1){
            // 表示已删除或不存在
            return AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS);
        }
        // 是否封贴
        if (article.getState()==1){
            // 状态已封贴
            return  AppResult.failed(ResultCode.FAILED_ARTICLE_BANNED);
        }
        // 构建回复对象
        ArticleReply articleReply=new ArticleReply();
        articleReply.setArticleId(articleId); // 要回复的帖id
        articleReply.setPostUserId(user.getId()); // 要回复的发送者
        articleReply.setContent(content); // 回复的内容

        // 写入回复
        articleReplyService.create(articleReply);
        // 返回结果
        return  AppResult.success();
    }

    @RequestMapping("/getReplies")
    public AppResult<List<ArticleReply>> getRepliesByArticleId(@RequestParam("articleId") @NonNull Long articleId){
        // 效验帖子是否存在
        Article article= articleService.selectById(articleId);
        if (article==null ||article.getDeleteState()==1){
            // 返回错误提示
            return AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS);
        }
        // 调用service
        List<ArticleReply> articleReplies=articleReplyService.selectByArticleId(articleId);
        // 返回结果
        return  AppResult.success(articleReplies);
    }
}
